package com.ruoyi.license.server;


import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

public class WindowsServerInfos extends AbstractServerInfos {

    @Override
    protected String getCPUSerial() throws Exception {
        //序列号
        String serialNumber = "";
        //使用WMIC获取CPU序列号
        Process process = Runtime.getRuntime().exec("wmic cpu get processorid");
        process.getOutputStream().close();
        Scanner scanner = new Scanner(process.getInputStream());
        if (scanner.hasNext()) {
            scanner.next();
        }
        if (scanner.hasNext()) {
            serialNumber = scanner.next().trim();
        }
        scanner.close();
        return serialNumber;
    }

    @Override
    protected String getMainBoardSerial() throws Exception {
        String serialNumber = getSysInfo("wmic baseboard get serialnumber");
        if (StringUtils.isEmpty(serialNumber)) {
            //如果获取不到主板序列号就获取bios序列号
            serialNumber = getSysInfo("wmic bios get serialnumber");
        }
        return serialNumber;
    }

    /**
     * 通过命令行获取系统信息
     *
     * @param command 查询信息的命令
     * @return 系统对应数据
     * @throws Exception 异常信息
     */
    private String getSysInfo(String command) throws Exception {
        String serialNumber = "";
        Process process = Runtime.getRuntime().exec(command);
        process.getOutputStream().close();
        Scanner scanner = new Scanner(process.getInputStream());
        if (scanner.hasNext()) {
            scanner.next();
        }
        if (scanner.hasNext()) {
            serialNumber = scanner.next().trim();
        }
        scanner.close();
        return serialNumber;
    }
}

